import ResourceLoader from "./js/base/ResourceLoader.js"

import Director from "./js/Director.js"
import DataStore from "./js/base/DataStore.js"

import Background from "./js/runtime/Background.js"
import Land from "./js/runtime/Land.js"
import Birds from "./js/player/Birds.js"
import StartButton from "./js/player/StartButton.js"
import Score from "./js/player/Score.js"

class Main {
  constructor() {
    this.canvas = document.getElementById("app")
    this.ctx = this.canvas.getContext("2d")
    const loader = ResourceLoader.create()
    loader.onloaded(map => { this.aLoader(map) })
    this.director = Director.getInstance()
    this.dataStore = DataStore.getInstance()
  }
  // 资源只会加载一次
  aLoader(map) {
    this.dataStore.ctx = this.ctx
    this.dataStore.res = map
    this.init()
  }

  init() {
    this.director.gameover = false
    this.dataStore
      .put("pencils", [])
      .put("background", new Background)
      .put("land", new Land)
      .put("birds", new Birds)
      .put("startButton", new StartButton)
      .put("score", new Score)
    this.director.createPencil()
    this.director.run()
    this.addEvent()
  }
  addEvent() {
    this.canvas.addEventListener("touchstart",e=>{
      e.preventDefault()
      if(this.director.gameover){
        console.log("重新开始")
        this.init()
      }else{
        this.director.birdsEvent()
      }
    })
  }
}

export default Main
